﻿Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Data.Linq
Imports System.Text
Imports DevComponents
Imports System.Data
Imports TNCDAL
Imports HTLibrary
Public Class ucTruck
    Dim db As New TanNamChinhDataContext
    Dim util As New Utilities
    Private isNewTruck As Boolean
    Private NewTruckID As Integer
    Private isEditTruck As Boolean
    Dim mtru As New MTrucks
    Dim indexRow As Integer = 0
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()
        DataGridViewHelper.DataGridView_InitEvent(grid)
        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Sub BindingToGrid()
        Dim query = mtru.getTrucksList2()
        If Not IsNothing(query) Then
            Dim source As New BindingSource
            source.DataSource = query
            BindingNavigator1.BindingSource = source
            grid.DataSource = source
            Try
                grid.CurrentCell.Selected = False
            Catch ex As Exception

            End Try
        End If
    End Sub

    Sub BindingToGridSearch()
        Dim query = mtru.getTrucksList2(txtTruckNoSearch.Text)
        If Not IsNothing(query) Then
            Dim source As New BindingSource
            source.DataSource = query
            BindingNavigator1.BindingSource = source
            grid.DataSource = source
            Try
                grid.CurrentCell.Selected = False
            Catch ex As Exception

            End Try
        End If
    End Sub

    Private Sub btnAddNewDri_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        isNewTruck = True
        isEditTruck = False
        EnableDriverControl(True, True)
        btnAdd.Enabled = False
        btnEdit.Enabled = False
        btnDelete.Enabled = False
        btnSave.Enabled = True
        btnCancel.Enabled = True
        txtTruckName.Focus()
        grid.Enabled = False
        indexRow = 0
    End Sub

    Sub EnableDriverControl(ByVal value As Boolean, ByVal addnew As Boolean)
        txtTruckName.Enabled = value
        txtTruckNumber.Enabled = value

        txtIDTruck.Enabled = value
        btnCancel.Enabled = value
        btnSave.Enabled = value
        chkIsActive.Enabled = value
        If isNewTruck Then

            txtTruckName.Text = ""

            txtTruckNumber.Text = ""
            txtIDTruck.Text = ""
            chkIsActive.Checked = True
            txtTruckName.Focus()
        Else

        End If

    End Sub

    Private Sub ResetControl()
        txtTruckName.Text = ""
        txtTruckNumber.Text = ""
        txtIDTruck.Text = ""
        chkIsActive.Checked = True
        txtTruckName.Focus()
    End Sub

    Private Sub FrmDriver_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Bar1.Text = "Search"
        Bar2.Text = "Details"
        BindingToGrid()
        'LoadSatuan()
        'isCellEdit = False
        isNewTruck = False
    End Sub

    Private Sub btnDelDri_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            Dim result As Integer
            result = util.ShowConfirm("Xác nhận", "Bạn chắc chắn muốn xoá xe: " & txtTruckName.Text)
            If result = DialogResult.Yes Then
                mtru.UpdateDeletedRecord(txtIDTruck.Text)
                BindingToGrid()
            End If
        Catch ex As Exception
            MessageBox.Show("Chưa chọn tài xế")
            Return
        End Try
        btnAdd.Enabled = True
        btnEdit.Enabled = True
        btnDelete.Enabled = True
        btnSave.Enabled = False
        btnCancel.Enabled = False

    End Sub

    Private Sub gridDriver_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grid.RowEnter
        Dim rw As Integer = e.RowIndex
        Dim dg As DataGridView = CType(sender, DataGridView)
        txtIDTruck.Text = dg.Item(0, rw).Value   'row.Item("id").ToString
        txtTruckName.Text = dg.Item(1, rw).Value 'row.Item("caption").ToString
        txtTruckNumber.Text = dg.Item(2, rw).Value 'row.Item("menu_desc").ToString
        chkIsActive.Checked = CBool(dg.Item(3, rw).Value)
    End Sub

    Private Sub btnEditDri_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        isNewTruck = False
        isEditTruck = True
        EnableDriverControl(True, True)
        btnAdd.Enabled = False
        btnEdit.Enabled = False
        btnDelete.Enabled = False
        btnSave.Enabled = True
        btnCancel.Enabled = True
        txtTruckName.Focus()
        grid.Enabled = False
        indexRow = grid.CurrentRow.Index
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        If isNewTruck Then
            'Thêm mới tài xế
            Dim obj As New tblTruck

            If txtTruckNumber.Text = "" Then
                MessageBox.Show("Nhập số xe")
                txtTruckNumber.Focus()
                Return
            End If
            obj.TruckName = txtTruckName.Text
            If txtTruckNumber.Text <> "" Then
                obj.TruckNumber = txtTruckNumber.Text

            End If
            obj.Type = 1
            obj.IsActive = True
            mtru.InsertNewRecord(obj)

        ElseIf isEditTruck Then
            'Sửa thông tin
            Dim obj As New tblTruck

            If txtTruckNumber.Text = "" Then
                MessageBox.Show("Nhập tên tài xế")
                txtTruckNumber.Focus()
                Return
            End If

            If txtIDTruck.Text = "" Then
                MessageBox.Show("Chưa chọn thông tin")
                txtTruckNumber.Focus()
                Return
            End If
            obj.ID_Truck = txtIDTruck.Text

            obj.TruckName = txtTruckName.Text

            If txtTruckNumber.Text <> "" Then
                obj.TruckNumber = txtTruckNumber.Text

            End If
            obj.IsActive = chkIsActive.Checked
            mtru.UpdateRecord(obj)

        End If
        isNewTruck = False
        isEditTruck = False
        BindingToGrid()
        EnableDriverControl(False, False)
        btnAdd.Enabled = True
        btnEdit.Enabled = True
        btnDelete.Enabled = True
        btnSave.Enabled = False
        btnCancel.Enabled = False
        grid.Enabled = True
        grid.Rows(indexRow).Selected = True
        grid.FirstDisplayedScrollingRowIndex = indexRow
        grid.CurrentCell = grid(1, indexRow)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        isNewTruck = False
        isEditTruck = False
        EnableDriverControl(False, False)
        BindingToGrid()
        btnAdd.Enabled = True
        btnEdit.Enabled = True
        btnDelete.Enabled = True
        btnSave.Enabled = False
        btnCancel.Enabled = False
        grid.Enabled = True
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        BindingToGridSearch()
    End Sub

    Private Sub btnCancelSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelSearch.Click
        BindingToGrid()
    End Sub

    Private Sub grid_DataBindingComplete(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles grid.DataBindingComplete

        If grid.RowCount > 0 Then
            btnEdit.Enabled = True
            btnDelete.Enabled = True
        Else
            ResetControl()
            btnEdit.Enabled = False
            btnDelete.Enabled = False
        End If

    End Sub

    Private Sub PanelExCommand_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PanelExCommand.Click

    End Sub

    Private Sub ExpandablePanelList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExpandablePanelList.Click

    End Sub

    Private Sub txtTruckNoSearch_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtTruckNoSearch.KeyDown
        If e.KeyCode = Keys.Enter Then
            BindingToGridSearch()
        End If
    End Sub
End Class
